package com.property.manage.controller.rent;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.property.manage.model.owner.OwnerEntity;
import com.property.manage.model.rent.RentVo;
import com.property.manage.service.rent.HouseRentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;

@Api(tags = "房屋出租管理")
@RestController
@RequestMapping("/houseRent")
@AllArgsConstructor
@Validated
@Slf4j
public class HouseRentController {
    @Autowired
    private HouseRentService houseRentService;
    @ApiOperation("更加楼栋id 回显租户信息")
    @PostMapping("/findOwner")
    public OwnerEntity findOwner(
                    @ApiParam("楼栋id")@RequestParam(value = "buildingId")@NotNull(message = "楼栋id不能为空") Integer buildingId
    ){
          return houseRentService.findOwner(buildingId);
    }

    @ApiOperation("添加出租信息")
    @PostMapping("/add")
    public void add(@ApiParam("小区id")@RequestParam(value = "villageId")@NotNull(message = "小区id不能为空") Integer villageId,
                    @ApiParam("租户名称")@RequestParam(value = "name")@NotBlank(message = "请先新增租户信息") String name,
                    @ApiParam("备注")@RequestParam(value = "remarks",required = false) String remarks,
                    @ApiParam("联系方式")@RequestParam(value = "phone") String phone,
                    @ApiParam("开始时间")@RequestParam(value = "startTime",required = false) LocalDate startTime,
                    @ApiParam("结束时间")@RequestParam(value = "endTime",required = false) LocalDate endTime,
                    @ApiParam("租金金额")@RequestParam(value = "money")@NotNull(message = "租金不能为空") BigDecimal money,
                    @ApiParam("物业费")@RequestParam(value = "propertyFee")@NotNull(message = "物业费不能为空") BigDecimal propertyFee,
                    @ApiParam("其他费用")@RequestParam(value = "otherFee")@NotNull(message = "其他费用不能为空") BigDecimal otherFee,
                    @ApiParam("楼栋id")@RequestParam(value = "buildingId",required = false)Integer buildingId,
                    @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId
    ){
      houseRentService.add(money,startTime,endTime,name,phone,buildingId,villageId,propertyFee,otherFee,remarks,userId);
    }


    @ApiOperation("租金列表")
    @GetMapping("/page")
    public CustomPage<RentVo> getPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                      @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                      @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                      @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                      @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                      @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber){
        return houseRentService.page(pageNo,pageSize,villageId,building,unit,roomNumber);
    }


    @PostMapping("/update")
    public void update(@RequestParam(value = "id")@NotNull(message = "id不能为空") Integer id,
            @ApiParam("小区id")@RequestParam(value = "villageId")@NotNull(message = "小区id不能为空") Integer villageId,
                    @ApiParam("租户名称")@RequestParam(value = "name")@NotBlank(message = "请先新增租户信息") String name,
                    @ApiParam("备注")@RequestParam(value = "remarks",required = false) String remarks,
                    @ApiParam("联系方式")@RequestParam(value = "phone") String phone,
                    @ApiParam("开始时间")@RequestParam(value = "startTime",required = false) LocalDate startTime,
                    @ApiParam("结束时间")@RequestParam(value = "endTime",required = false) LocalDate endTime,
                    @ApiParam("租金金额")@RequestParam(value = "money")@NotNull(message = "租金不能为空") BigDecimal money,
                    @ApiParam("物业费")@RequestParam(value = "propertyFee")@NotNull(message = "物业费不能为空") BigDecimal propertyFee,
                    @ApiParam("其他费用")@RequestParam(value = "otherFee")@NotNull(message = "其他费用不能为空") BigDecimal otherFee,
                    @ApiParam("楼栋id")@RequestParam(value = "buildingId",required = false)Integer buildingId,
                    @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId
    ){
        houseRentService.update(money,startTime,endTime,name,phone,buildingId,villageId,propertyFee,otherFee,remarks,userId,id);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public void delete(@ApiParam("id") @RequestParam("id") @NotNull(message = "id为空") Integer id){
        houseRentService.delete(id);
    }
    @ApiOperation("批量划扣")
    @PostMapping("/deduction")
    public void deduction(@ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId,
                          @ApiParam("小区编号") @RequestParam("villageId") @NotNull(message = "小区编号为空") Integer villageId,
                          @ApiParam("划扣年份") @RequestParam("year") @NotNull(message = "划扣年份为空") Integer year,
                          @ApiParam("划扣月份") @RequestParam("month") @NotNull(message = "划扣月份为空") Integer month){

    houseRentService.deduction(userId, villageId, year, month);

    }


}
